/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.prototype.console;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/**.
 * Treat connection to SourceForge website
 * @author zineddine
 */
public final class Connection {
    /**.
    * @param urlPrincipale
    */
    private String urlPrincipale;
    /**.
    * @param sfPage contains a page's HTML code
    */
    private Document sfPage;
    /**.
    * @param concurrentMap
    */
    private ConcurrentHashMap<String, String> concurrentMap;

    /**.
    * Constructor
    * @param url
    */
    public Connection(final String url) {
        try {
            this.urlPrincipale = url;
            this.sfPage = Jsoup.parse(new URL(urlPrincipale).openStream(),
                    "ISO-8859-1", urlPrincipale);
//            this.sfPage = Jsoup.connect(urlPrincipale).timeout(0).get();
            this.concurrentMap = new ConcurrentHashMap<String, String>();
            parsePage(urlPrincipale);
        } catch (IOException ex) {
            Logger.getLogger(Connection.class.getName())
                    .log(Level.SEVERE, null, ex);
        }
    }

    /**.
     * @return concurrentMap
     */
    public ConcurrentHashMap<String, String> getConcurrentMap() {
        return concurrentMap;
    }

    /**.
     * returns the number of pages which contain projects
     * @return nbPage
     */
    public int nbPage() {
        int nbPage = 0;
        String oneofnbpage = sfPage.getElementById("result_count").text();
        int last = oneofnbpage.lastIndexOf('.');
        int lastvide = oneofnbpage.lastIndexOf(' ');
        lastvide++;
        String s = oneofnbpage.substring(lastvide, last);
        if (Integer.parseInt(s) != 0) {
            nbPage = Integer.parseInt(s);
        }
        return nbPage;
    }

    /**.
     *
     * @param urlPage
     * @throws IOException
     */
    public void parsePage(final String urlPage) throws IOException {
        sfPage = Jsoup.connect(urlPage).timeout(0).get();
        Element resultat = sfPage.getElementById("result_data");
        Elements links = resultat.getElementsByTag("a");

        for (Element link : links) {

            String linkHref = link.attr("href");
            String linkText = link.text();
            concurrentMap.put(linkText, linkHref);
       }
    }

    /**.
     * as the research returns many projects coming from different page,
     * we should treat each page one by one
     * @return UrlsPages
     */
    public List<String> urlPage() {
        final String otherPage = "?page=";
        ArrayList<String> urlsPages = new ArrayList<String>();
        // to have the number of pages
        int nbpage = nbPage();
        if (nbpage != 1) {
            for (int i = 2; i <= nbpage; i++) {
                    String s = Integer.toString(i);
                    urlsPages.add(getUrlPrincipale() + otherPage + s);
            }
        }
        return urlsPages;
    }

    /**.
     * @return urlPrincipale
     */
    public String getUrlPrincipale() {
        return urlPrincipale;
    }

    /**.
     * @return SFPage
     */
    public Document getSfPage() {
        return sfPage;
    }
}
